﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Configuration;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Net;
namespace OF.Utility
{
    public class HtmlUtility
    {

        #region 清除HTML格式

        public static string CutHref(string strHtml, string href, string text)
        {
            if (href != "")
            {
                Regex reg = new Regex(@"(?is)<a(?:(?!href=).)*href=(['""]?)(?<url>[^""\s>]*)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");
                MatchCollection mc = reg.Matches(strHtml);
                string strUrl = "";
                string strTitle = "";
                foreach (Match m in mc)
                {
                    strUrl = m.Groups["url"].Value;
                    strTitle = m.Groups["text"].Value;
                    if (href != "")
                        strHtml = strHtml.Replace(strUrl, href);

                    if (text != "")
                        strHtml = strHtml.Replace(strTitle, text);
                }

            }
            else
            {
                strHtml = System.Text.RegularExpressions.Regex.Replace(strHtml, @"(?is)<a(?:(?!href=).)*href=(['""]?)(?<url>[^""\s>]*)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            }
            return strHtml;
        }

        /// <summary>
        /// 过滤html
        /// </summary>
        /// <param name="strConnect">要过滤的内容</param>
        /// <param name="flg">【1：过滤脚本】</param>
        /// <returns>过滤后的值</returns>
        public static string strJavaScriptClear(string strhtml)
        {
            strhtml = System.Text.RegularExpressions.Regex.Replace(strhtml, @"<script[^>]*?>.*?</script>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            //return strhtml.Replace(" ", "");
            return strhtml;
        }
        /// <summary>
        /// 过滤html
        /// </summary>
        /// <param name="strConnect">要过滤的内容</param>
        /// <param name="flg">【1：过滤脚本】</param>
        /// <returns>过滤后的值</returns>
        public static string strDomClear(string strhtml, string strStartL, string strEndL, string strContent)
        {
            //<[\\s]*?div class=\"qu_peibu\">[\\s\\S]+<[\\s]*?/div[\\s]*?>
            string strReg = string.Format("{0}[\\s\\S]+{1}", strStartL, strEndL);
            strhtml = System.Text.RegularExpressions.Regex.Replace(strhtml, strReg, strContent, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            return strhtml;
        }
        /// <summary>
        /// Html标签过滤
        /// </summary>
        /// <param name="ctx">内容</param>
        /// <returns>返回过滤后的内容</returns>
        public static string RemoveSpecifyHtml(string ctx, string strDom)
        {
            if (string.IsNullOrEmpty(strDom))
                strDom = "abc";
            string[] holdTags = strDom.Split(',');//保留的 tag  
            //string[] holdTags = { "img", "br", "strong", "b", "span", "li", "p" };//保留的 tag  
            // <(?!((/?\s?li\b)|(/?\s?ul\b)|(/?\s?a\b)|(/?\s?img\b)|(/?\s?br\b)|(/?\s?span\b)|(/?\s?b\b)))[^>]+>  
            string regStr = string.Format(@"<(?!((/?\s?{0})))[^>]+>", string.Join(@"\b)|(/?\s?", holdTags));
            Regex reg = new Regex(regStr, RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);
            return reg.Replace(ctx, "");
        }
        /// <summary>
        /// Html标签过滤
        /// </summary>
        /// <param name="ctx">内容</param>
        /// <returns>返回过滤后的内容</returns>
        public static string RemoveAllHtml(string ctx)
        {
            string[] holdTags = { "o" };//保留的 tag  
            // <(?!((/?\s?li\b)|(/?\s?ul\b)|(/?\s?a\b)|(/?\s?img\b)|(/?\s?br\b)|(/?\s?span\b)|(/?\s?b\b)))[^>]+>  
            string regStr = string.Format(@"<(?!((/?\s?{0})))[^>]+>", string.Join(@"\b)|(/?\s?", holdTags));
            Regex reg = new Regex(regStr, RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);
            return reg.Replace(ctx, "");
        }
        /// <summary>
        /// Html标签过滤
        /// </summary>
        /// <param name="ctx">内容</param>
        /// <returns>返回过滤后的内容</returns>
        public static string ImagesSave(string strHTML, string strPath, string strUrl, out string strImgUrl, string MainUrl)
        {
            string url = "";
            strImgUrl = "";
            try
            {
                Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
                //Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
                MatchCollection mc = regImg.Matches(strHTML);
                int i = 0;
                FileUtility.CreateDirectory(strPath);
                if (mc.Count > 0)
                {
                    foreach (Match m in mc)
                    {
                        url = m.Groups[1].Value;

                        if (url.IndexOf("http://") == -1)
                        {
                            url = MainUrl + url;
                        }
                        WebClient mywebclient = new WebClient();
                        string FileName = FileUtility.GetFileName(Path.GetExtension(url));
                        string Temp = strPath + FileName;
                        mywebclient.DownloadFile(url, Temp);

                        string strimg = "src=\"" + m.Groups[1].Value + "\"";
                        string strbaksrc = " src=\"" + SiteUtility.GetAppSettings("noImage") + "\" ";
                        string img = strbaksrc + " baksrc=\"" + strUrl + FileName + "\" class=\"on\" ";

                        if (i < 2)
                        {
                            img = "src=\"" + strUrl + FileName + "\"";
                        }
                        if (strImgUrl == "")
                            strImgUrl = strUrl + FileName;

                        strHTML = strHTML.Replace(strimg, img);
                        i++;
                    }
                }

            }
            catch (Exception ex)
            {
                FileUtility.WriteErrorLog(ex.Message.ToString() + "strUrl=" + url, "");
            }
            return strHTML;
        }
        #endregion
        /// <summary>
        /// 静态方法：获取请求的url的html源码
        /// <para>最多请求三次，三次都没有html源码就跳出</para>
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="method">Post或者Get</param>
        /// <param name="pEncoding">编码格式</param>
        /// <returns>html源码(String类型数据)</returns>
        public static string GetHtml(string url, string method, Encoding pEncoding)
        {
            string _str = string.Empty;
            int num = 0;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            #region 模拟百度爬虫
            request.UserAgent = "User-Agent: Mozilla/5.0+(Windows+NT+6.0;+WOW64;+rv:31.0)+Gecko/20100101+Firefox/31.0";
            //Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0
            request.Accept = "*/*";
            request.KeepAlive = true;
            request.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
            #endregion

            request.Method = method;

            //给post请求增加请求数据
            if (method.ToLower() == "post")
            {
                string data = url.Substring(url.IndexOf("?") + 1);
                byte[] byteArray = pEncoding.GetBytes(data);
                request.ContentLength = byteArray.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(byteArray, 0, byteArray.Length);
                stream.Close();
            }

            //request.Method = method;
            request.MaximumAutomaticRedirections = int.MaxValue;
            //允许请求重定向  默认值为false
            request.AllowAutoRedirect = true;
            request.Timeout = 30000;
            while (num < 3 && string.IsNullOrEmpty(_str))
            {
                try
                {
                    HttpWebResponse objResponse = request.GetResponse() as HttpWebResponse;
                    Stream objStream = objResponse.GetResponseStream();
                    objStream.ReadTimeout = 5000;
                    StreamReader objStreamReader = null;
                    if (objResponse.ContentEncoding != null && objResponse.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        objStreamReader = new StreamReader(new GZipStream(objStream, CompressionMode.Decompress), Encoding.GetEncoding(936));
                    else
                        objStreamReader = new StreamReader(objStream, pEncoding);

                    objStream.ReadTimeout = 15000;
                    _str = objStreamReader.ReadToEnd();
                    objStreamReader.Close();
                    objStreamReader.Dispose();
                    objResponse.Close();
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    num++;
                }
            }
            return _str;
        }
    }
}
